package com.xiaodu.acer.phoneplay.ui.fragment;


import android.annotation.SuppressLint;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ms.square.android.expandabletextview.ExpandableTextView;
import com.xiaodu.acer.phoneplay.R;
import com.xiaodu.acer.phoneplay.bean.AppInfo;
import com.xiaodu.acer.phoneplay.common.Constant;
import com.xiaodu.acer.phoneplay.common.util.DateUtils;
import com.xiaodu.acer.phoneplay.di.component.AppComponent;
import com.xiaodu.acer.phoneplay.di.component.DaggerAppDetailComponent;
import com.xiaodu.acer.phoneplay.di.module.AppDetailModule;
import com.xiaodu.acer.phoneplay.presenter.AppDetailPresenter;
import com.xiaodu.acer.phoneplay.presenter.contract.AppDetailContract;
import com.xiaodu.acer.phoneplay.ui.adapter.AppInfoAdapter;
import com.xiaodu.acer.phoneplay.ui.base.BaseFragment;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import library.imageloader.ImageLoader;

/**
 * 为了解耦，分担Activity的代码量
 */
@SuppressLint("ValidFragment")
public class AppDetailFragment extends BaseFragment<AppDetailPresenter> implements AppDetailContract.View {

    @BindView(R.id.view_gallery)
    LinearLayout mViewGallery;
    @BindView(R.id.expandable_text)
    TextView mExpandableText;
    @BindView(R.id.expand_collapse)
    ImageButton mExpandCollapse;
    @BindView(R.id.view_introduction)
    ExpandableTextView mViewIntroduction;
    @BindView(R.id.txt_update_time)
    TextView mTxtUpdateTime;
    @BindView(R.id.txt_version)
    TextView mTxtVersion;
    @BindView(R.id.txt_apk_size)
    TextView mTxtApkSize;
    @BindView(R.id.txt_publisher)
    TextView mTxtPublisher;
    @BindView(R.id.txt_publisher2)
    TextView mTxtPublisher2;
    @BindView(R.id.recycler_view_same_dev)
    RecyclerView mRecyclerViewSameDev;
    @BindView(R.id.recycler_view_relate)
    RecyclerView mRecyclerViewRelate;
    private int mAppId;

    private AppInfoAdapter mAdapter;
    private LayoutInflater mInflater;


    @SuppressLint("ValidFragment")
    public AppDetailFragment(int id) {
        this.mAppId = id;
    }

    @Override
    public int setLayout() {
        return R.layout.fragment_app_detail;
    }

    @Override
    public void setupAcitivtyComponent(AppComponent appComponent) {
        DaggerAppDetailComponent.builder()
                .appDetailModule(new AppDetailModule(this))
                .appComponent(appComponent)
                .build()
                .inject(this);
    }

    @Override
    public void init() {
        mInflater = LayoutInflater.from(getActivity());
        mPresenter.getAppDetail(mAppId);
    }

    @Override
    public void showAppDetail(AppInfo appInfo) {

        showScreenshot(appInfo.getScreenshot());

        mViewIntroduction.setText(appInfo.getIntroduction());

        mTxtUpdateTime.setText(DateUtils.formatDate(appInfo.getUpdateTime()));
        mTxtApkSize.setText((appInfo.getApkSize() / 1014 / 1024) + " Mb");
        mTxtVersion.setText(appInfo.getVersionName());
        mTxtPublisher.setText(appInfo.getPublisherName());
        mTxtPublisher2.setText(appInfo.getPublisherName());


        mAdapter = AppInfoAdapter.builder()
                .layout(R.layout.template_appinfo2)
                .build();

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
        mRecyclerViewSameDev.setLayoutManager(layoutManager);


        mAdapter.addData(appInfo.getSameDevAppInfoList());
        mRecyclerViewSameDev.setAdapter(mAdapter);

        /////////////////////////////////////////////
        //两个adapter不可复用
        mAdapter = AppInfoAdapter.builder()
                .layout(R.layout.template_appinfo2)
                .build();
        //两个LinearLayoutManager不可复用
        mRecyclerViewRelate.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
        mAdapter.addData(appInfo.getRelateAppInfoList());
        mRecyclerViewRelate.setAdapter(mAdapter);

    }

    /**
     * 头部下边的画廊
     *
     * @param screentShot
     */
    private void showScreenshot(String screentShot) {


        List<String> urls = Arrays.asList(screentShot.split(","));

        for (String url : urls) {
            ImageView imageView = (ImageView) mInflater.inflate(R.layout.template_imageview, mViewGallery, false);
            imageView.setAdjustViewBounds(true);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(240, 320);
            imageView.setLayoutParams(layoutParams);

            ImageLoader.load(Constant.BASE_IMG_URL + url, imageView);

            mViewGallery.addView(imageView);

        }


    }


}
